//订单筛选页面
var calendarDateFormat = myApp.calendar({
    input: '#beginTime',
    closeOnSelect:true,
});
var calendarDateFormat = myApp.calendar({
    input: '#endTime',
    closeOnSelect:true,
});
(function(){
	var orderFilter={
		init:function(){
			myApp.showIndicator();
			args = GetUrlParms();				
			if(args["type"]!=undefined){
				TYPE=args["type"];
			}
			this.bindEvents();
			this.loadParams();
//			this.CustomerOrderStatusNum();
		},
		bindEvents:function(){
			var self=this;
			//订单状态的选中
//			$("#OrderStatus span").on("click",function(){
//				$(this).addClass("active").siblings().removeClass("active");
//			});
			//参数的选中
			$("#order_term_box").on("click",".term span",function(){
				$(this).toggleClass("active").siblings().removeClass("active");
			});
			//确定
			$("#confirm_btn").on("click",function(){
				var orderJson={};
            	orderJson.orderParams = self.getParameters();
            	if(TYPE==1){
            		var orderUrl="/Customer/DiaOrder";
            	}if(TYPE==2){
            		var orderUrl="/Customer/ParcelOrder";
            	}else if(TYPE==3){
            		var orderUrl="/Customer/RingOrder";
            	}else if(TYPE==4){
            		var orderUrl="/Customer/GemOrder";
            	}else if(TYPE==5){
            		var orderUrl="/Customer/EmeraldOrder";
            	}else if(TYPE==6){
            		var orderUrl="/Customer/PrimeGoldOrder";
            	}
	            if(APP){
	            	var appJson={};
	            	appJson.title=_("订单");
	            	appJson.url=app_url+orderUrl;
	            	appJson.content=JSON.stringify(orderJson);
	            	YiYi_APP.submitOrder(JSON.stringify(appJson))

	            }else{
	            	var action=orderUrl;
				  	form = $("<form method='post' id='params_form' action='"+action+"'>"+
				   	"<input type='text' value='"+JSON.stringify(orderJson)+"' name='params'></form>")
					$("#form_box").html(form);
					$("#params_form").submit();

	            	//localStorage.setItem('cusOrderList',JSON.stringify(orderJson))
	            }


			});
			
		},
		//统计订单状态的个数
		CustomerOrderStatusNum:function(){
            var requestOrder = {};
            requestOrder.CustomerID = CustomerID;
            requestOrder.UserID=UserID;
            $.ajax({
                url: "/Team/OrderNumber",//提交地址
                type: "POST",//提交方式
                data: JSON.stringify(requestOrder),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    var _result = eval('(' + result + ')');//eval()解析单个json对象
                    var total = 0;
                    if(_result.msgcode== true){
                        _result=_result.msginfo;
                        for (var i = 0; i < _result.length; i++) {
                            if (_result[i].OrderStatus == 1) {
                                $("#status1").text(_result[i].Number);
                                total += _result[i].Number;
                            }
                            if (_result[i].OrderStatus == 2) {
                                $("#status2").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            }
                            if (_result[i].OrderStatus == 3) {
                                $("#status3").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            } 
                            if (_result[i].OrderStatus == 4) {
                                $("#status4").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            } 
                            if (_result[i].OrderStatus == 5) {
                                $("#status5").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            }
                            if (_result[i].OrderStatus == 6) {
                                $("#status6").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            }
                            $("#status0").html(total);//订单总数
                        }
                    }else{
                        $("#status0").text(total);//订单总数
                        $("#status1").text(0);//订单
                        $("#status2").text(0);//订单
                        $("#status3").text(0);//订单
                        $("#status4").text(0);//订单
                        $("#status5").text(0);//订单
                        $("#status6").text(0);//订单
                    }
                }
            });
        },
				 //获取筛选参数
        getParameters: function () {
        	
            var requestOrder = {};
            requestOrder.CustomerID = CustomerID;//客户ID
            requestOrder.PageSize = 100;
            requestOrder.PageIndex = 1;
            requestOrder.OrderStatus = $("#OrderStatus").find("span.active").attr("data-value");//订单状态
            requestOrder.SCreateTime = $("#beginTime").val();//开始时间
            requestOrder.ECreateTime = $("#endTime").val();//结束时间
            
            if(TYPE>2 || TYPE==1){
            	 requestOrder.CertNo = $.trim($("#certNo").val());//证书号
            	 if(TYPE==1){
            	 	 requestOrder.CertType = $("#certType").find("span.active").attr("data-value");//证书
            	 }else{
            	 	 requestOrder.CertType = $.trim($("#certType").val());//证书号
            	 }            	
            }
           	if(TYPE==1){
           		requestOrder.Color = $("#Color").find("span.active").attr("data-value");//颜色
            	requestOrder.Clarity = $("#Clarity").find("span.active").text();//净度
           	}
            requestOrder.OriginSN = $.trim($("#goodsNum").val());//货号
            requestOrder.Weight = $("#weightNo").val();//重量
           // requestOrder.Color = $("#Color").find("span.active").text();//颜色
            //requestOrder.Clarity = $("#Clarity").find("span.active").text();//净度
            //requestOrder.CertType = $("#CertType").find("span.active").text();//证书类型
            requestOrder.PaymentStatus = $("#PaymentStatus").find("span.active").attr("data-value");//支付状态
            return requestOrder;
        },
		loadParams:function(){
			if(TYPE==1){
				var $certNo=[
						'<div class="term">',
	                        '<h4>'+_("证书号")+'</h4>',
	                       '<input type="text" class="cert-num" id="certNo" placeholder="'+_("请输入证书号")+'" />',
	                    '</div>'                   
					].join('');								
				$("#originSN_box").after($certNo);
				jsonType=stock;
				var $color='<div class="term color" id="Color"><h4>'+_("颜色")+'</h4>';
				//颜色
				for(var i=0;i<jsonType.length;i++){
					if(jsonType[i].Color){
						for(var j=0;j<jsonType[i].Color.length;j++){
							$color += '<span data-value="'+jsonType[i].Color[j][0]+'">'+ _(jsonType[i].Color[j][1]) +'</span>';
						}
					}
				}
				$color+='</div>';
				$("#weight_box").after($color)
				//净度
				var $Clarity='<div class="term clarity" id="Clarity"><h4>'+_("净度")+'</h4>';
				for(var j=0;j<jsonType[1].Clarity.length;j++){
					$Clarity += '<span>'+ _(jsonType[1].Clarity[j][1]) +'</span>';
				}
				$Clarity +='</div>';
				$("#Color").after($Clarity);
				//证书
				var $CertType='<div class="term" id="certType"><h4>'+_("证书")+'</h4>';
				for(var j=0;j<jsonType[1].CertType.length;j++){
					$CertType += '<span data-value="'+jsonType[1].CertType[j][0]+'">'+ _(jsonType[1].CertType[j][1]) +'</span>';
				}
				$CertType +='</div>';
				$("#Clarity").after($CertType);
			}else if(TYPE==2){
				//大小
				var size=[
					'<div class="term">',
	                    '<h4>'+_("大小")+'</h4>',
	                    '<span data-value="0.001-0.009">'+_("厘石")+'</span>',
	                    '<span data-value="0.01-0.02">'+_("分石")+'</span>',
	                    '<span data-value="0.03-0.07">0.05</span>',
	                    '<span data-value="0.08-0.12">0.10</span>',
	                    '<span data-value="0.13-0.17">0.15</span>',
	                    '<span data-value="0.18-0.22">0.20</span>',
	                    '<span data-value="0.23-0.29">0.25</span>',
	                    '<span data-value="0.30-0.39">0.30</span>',
	                    '<span data-value="0.40-0.49">0.40</span>',
	                    '<span data-value="0.50-0.69">0.50</span>',
	                    '<span data-value="0.70-0.99">0.70</span>',
	                    '<span data-value="1.00-1.99">1.00</span>',
	                '</div>'   
				].join('');
				$("#weight_box").after(size);
				var color="<h4>"+_("颜色")+"</h4>";
				//debugger
				for(var i=0;i<parcel.length;i++){
					if(parcel[i].Color){
						for(var j=0;j<parcel[i].Color.length;j++){
							color += '<span data-value="'+parcel[i].Color[j][0]+'">'+ (parcel[i].Color[j][1]) +'</span>';
						}
					}
				}
				//$("#Color").html(color)

			}else if(TYPE>2 ){
				//证书号
				if(TYPE==6){
					var $certNo=[
					 	'<div class="term">',
	                        '<h4>'+_("证书类型")+'</h4>',
	                       '<input type="text" class="cert-num" id="certType" placeholder="'+_("请输入证书类型")+'" />',
	                    '</div>',                  
					].join('');			
				}else{
					var $certNo=[
					 	'<div class="term">',
	                        '<h4>'+_("证书类型")+'</h4>',
	                       '<input type="text" class="cert-num" id="certType" placeholder="'+_("请输入证书类型")+'" />',
	                    '</div>',
						'<div class="term">',
	                        '<h4>'+_("证书号")+'</h4>',
	                       '<input type="text" class="cert-num" id="certNo" placeholder="'+_("请输入证书号")+'" />',
	                    '</div>'                   
					].join('');					
				}
				
				$("#originSN_box").after($certNo);
			}
			myApp.hideIndicator();
		},
	}
	orderFilter.init();
})();
